import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { list, delOperlog } from '@/api/monitor/operlog';

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'operId'
  const moduleName =  '操作日志'
  const apiFn = {
    listApiFn: list,
    delApiFn: delOperlog,
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_oper_type',
    'sys_common_status'
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: "系统模块",
      prop: "title",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作类型",
      prop: "businessType",
      valueType: "select",
      tableColumnProps: {
        align: "center"
      },
      options: dictRawData.sys_oper_type
    },
    {
      label: "请求方式",
      prop: "requestMethod",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作人员",
      prop: "operName",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作地址",
      prop: "operIp",
      tableColumnProps: {
        align: "center"
      }

    },
    {
      label: "操作状态",
      prop: "status",
      valueType: "select",
      tableColumnProps: {
        align: "center"
      },
      options: dictRawData.sys_common_status
    },
    {
      label: "操作日期",
      prop: "operTime",
      tableColumnProps: {
        align: "center"
      },
      valueType: "date-picker",
      fieldProps: {
        type: "daterange",
        format: "YYYY-MM-DD"
      },
      render: val=> proxy.parseTime(val)
    },
    {
      label: "消耗时间",
      prop: "costTime",
      tableColumnProps: {
        align: "center"
      },
      hideInSearch: true,
      render: (value) => {
        return value + "ms";
      }
    },
    {
      label: "操作",
      prop: "action",
      width: 100,
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

